package com.chushouya.product.service.api.impl;

import com.chushouya.product.dao.entity.ProductImageRecordEntity;
import com.chushouya.product.dao.repository.ProductImageRecordRepository;
import com.chushouya.product.dto.api.home.DouyinHomeDataDTO;
import com.chushouya.product.service.api.ProductImageApiService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 产品图片API服务实现类
 * 
 * @author chushouya
 */
@Service
public class ProductImageApiServiceImpl implements ProductImageApiService {

    @Resource
    private ProductImageRecordRepository productImageRecordRepository;

    @Override
    public DouyinHomeDataDTO getDouyinHomeData() {
        DouyinHomeDataDTO result = new DouyinHomeDataDTO();
        
        // 获取当天日期，格式：2025-11-5
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
        String today = sdf.format(new Date());
        
        // 查询当天成功的产品图片记录
        ProductImageRecordRepository.ProductImageRecordQuery query = new ProductImageRecordRepository.ProductImageRecordQuery();
        query.setStatus(1); // 只查询成功的记录
        query.setDate(today); // 只查询当天的记录
        List<ProductImageRecordEntity> records = productImageRecordRepository.selectList(query);
        
        // 按分类分组
        Map<Long, DouyinHomeDataDTO.CategoryDTO> categoryMap = new LinkedHashMap<>();
        
        for (ProductImageRecordEntity record : records) {
            Long categoryId = record.getCategoryId();
            String categoryName = record.getCategoryName();
            Long brandId = record.getBrandId();
            String brandName = record.getBrandName();
            String brandImage = record.getBrandImage();
            Integer productCount = record.getProductCount();
            String ossUrl = record.getOssUrl();
            
            // 获取或创建分类
            DouyinHomeDataDTO.CategoryDTO category = categoryMap.get(categoryId);
            if (category == null) {
                category = new DouyinHomeDataDTO.CategoryDTO();
                category.setCategoryId(categoryId);
                category.setCategoryName(categoryName);
                category.setBrands(new ArrayList<>());
                categoryMap.put(categoryId, category);
            }
            
            // 检查品牌是否已存在
            boolean brandExists = category.getBrands().stream()
                    .anyMatch(b -> b.getBrandId().equals(brandId));
            
            if (!brandExists) {
                // 添加品牌
                DouyinHomeDataDTO.BrandDTO brand = new DouyinHomeDataDTO.BrandDTO();
                brand.setBrandId(brandId);
                brand.setBrandName(brandName);
                brand.setBrandImage(brandImage);
                brand.setProductCount(productCount);
                brand.setOssUrl(ossUrl);
                category.getBrands().add(brand);
            }
        }
        
        // 转换为列表并按分类ID升序排序
        List<DouyinHomeDataDTO.CategoryDTO> categories = categoryMap.values().stream()
                .sorted(Comparator.comparing(DouyinHomeDataDTO.CategoryDTO::getCategoryId))
                .collect(Collectors.toList());
        result.setCategories(categories);
        
        return result;
    }
}
